/**
 * @Author yinyunqi
 * @date 2018年1月31日
 * @Content 
 */
package com.yongqi.task;

import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import com.yongqi.model.back.Email;
import com.yongqi.model.console.Debt;
import com.yongqi.pub.DateFormat;
import com.yongqi.pub.EmailHelper;
import com.yongqi.service.back.EmailService;
import com.yongqi.service.console.DebtService;

@Component
@Configuration
@EnableScheduling
public class ScheduleTask {
    @Autowired 
    EmailService emailService;
    @Autowired
    DebtService debtService;
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);
    public void sayHello() {
	System.out.println("-----------------ScheduleTask-----------------");
	LOGGER.info("helllllllllllllllllllllllllllllllllllllllllllllllll");
    }
    /**
     * 定时任务：每天中午12点运行，发送邮件
     */
    public void sendEmail() {
    	List<Debt> endedDebtList = debtService.overTimeDebt();
    	
    	for (Debt debt : endedDebtList) {
    		if(debt.getBorrowemail()!=null && debt.getLoanmail()!=null && debt.getBorrowemail()!="" && debt.getLoanmail()!="") {
	    		if (debt.getDebttype().equals("借入")) {
	    			//借入逻辑：发邮件提醒用户该还钱了，发邮件提醒借钱用户该收钱了
	    			//将该邮件存入数据库中，待用户登录可看到
	    			
	    			//发邮件给本用户
	    			String emailAccount = debt.getBorrowemail();	
	    			String emailSubject = "你有一笔欠款待还";
	    			String emailContent = "你有一笔项目名为: "+debt.getItem()+" ，借自 "+
	    					debt.getLoanmember()+" ,金额为 "+debt.getMoney()+" 的借款待还。"+
	    					"</br>发自家庭记账管理系统</br>感谢你的使用";
	    			EmailHelper.sendEmail(emailAccount, emailSubject, emailContent);
	    			
	    			//发邮件给借钱用户
	    			String emailAccount2 = debt.getLoanmail();	
	    			String emailSubject2 = "你有一笔欠款待收";
	    			String emailContent2 = "你有一笔项目借给 "+
	    					debt.getBorrowmember()+" ,金额为 "+debt.getMoney()+" 的借款待收。"+
	    					"</br>发自家庭记账管理系统</br>感谢你的使用";
	    			EmailHelper.sendEmail(emailAccount2, emailSubject2, emailContent2);
	    			
	    			//存入数据库中
	    			Email email = new Email();
	    			email.setId(UUID.randomUUID().toString());
	    			email.setEmail_family(debt.getFamilyaccount());		//
	    			email.setEmail_to(emailAccount);
	    			email.setEmail_subject(emailSubject);
	    			email.setEmail_content(emailContent);
	    			email.setCreated_time(DateFormat.dateFormate());
	    			email.setEmail_account("familyfinancial@163.com");
	    			emailService.addEmail(email);
	    			
				}else if(debt.getDebttype().equals("借出")){
					//借出逻辑：发邮件提醒用户该还钱了，发邮件提醒借钱用户该收钱了
	    			//将该邮件存入数据库中，待用户登录可看到
					
					//发邮件给借出用户
	    			String emailAccount = debt.getLoanmail();	
	    			String emailSubject = "你有一笔欠款待收";
	    			String emailContent = "你有一笔项目名为:"+debt.getItem()+"，借给 "+
	    					debt.getBorrowmember()+",金额为"+debt.getMoney()+"的借款待收"+
	    					"</br>发自家庭记账管理系统</br>感谢你的使用";
//	    			String emailSubject = "从前有座庙";
//	    			String emailContent = "庙里有座山</br>这是一封自动发送，占着仙气儿的邮件";
	    			EmailHelper.sendEmail(emailAccount, emailSubject, emailContent);
	    			
	    			//发邮件给借给用户
	    			String emailAccount2 = debt.getBorrowemail();
	    			String emailSubject2 = "你有一笔欠款待还";
	    			String emailContent2 = "你有一笔借自"+
	    					debt.getLoanmember()+",金额为"+debt.getMoney()+"的借款待还"+
	    					"</br>发自家庭记账管理系统</br>感谢你的使用";
//	    			String emailSubject2 = "从前有座山";
//	    			String emailContent2 = "庙里有座山</br>这是一封自动发送，占着仙气儿的邮件";
	    			EmailHelper.sendEmail(emailAccount2, emailSubject2, emailContent2);
	    			
	    			//存入数据库中
	    			Email email = new Email();
	    			email.setId(UUID.randomUUID().toString());
	    			email.setEmail_family(debt.getFamilyaccount());		//
	    			email.setEmail_to(emailAccount);
	    			email.setEmail_subject(emailSubject);
	    			email.setEmail_content(emailContent);
	    			email.setCreated_time(DateFormat.dateFormate());
	    			email.setEmail_account("familyfinancial@163.com");
	    			emailService.addEmail(email);
				}
    		}
		}
    }
}

